// Copyright (c) 2012, yuno. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of contributors
//    may be used to endorse or promote products derived from this software
//    without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
package sugarmintcandy.widgetwindow;

import android.graphics.Point;
import android.graphics.Rect;
import android.util.DisplayMetrics;

public enum Anchor {
    NONE, UPPER_LEFT, UPPER_RIGHT, LOWER_LEFT, LOWER_RIGHT;

    public Rect decodeRect(DisplayMetrics dm, int x, int y, int width,
	    int height) {
	Rect r = new Rect();
	switch (this) {
	case UPPER_LEFT:
	    r.left = x;
	    r.top = y;
	    break;
	case UPPER_RIGHT:
	    r.left = x + dm.widthPixels - width;
	    r.top = y;
	    break;
	case LOWER_LEFT:
	    r.left = x;
	    r.top = y + dm.heightPixels - height;
	    break;
	case LOWER_RIGHT:
	    r.left = x + dm.widthPixels - width;
	    r.top = y + dm.heightPixels - height;
	    break;
	default:
	    if (x >= (1 << 16)) {
		r.left = dm.widthPixels - width + x - (1 << 16);
	    } else if (x > 0) {
		r.left = (x * (dm.widthPixels - width) + (1 << 15)) >> 16;
	    }
	    if (y >= (1 << 16)) {
		r.top = dm.heightPixels - height + y - (1 << 16);
	    } else if (y > 0) {
		r.top = (y * (dm.heightPixels - height) + (1 << 15)) >> 16;
	    }
	    break;
	}
	r.right = r.left + width;
	r.bottom = r.top + height;
	return r;
    }

    public Point encodePoint(DisplayMetrics dm, int left, int top, int right,
	    int bottom) {
	Point p = new Point();
	switch (this) {
	case UPPER_LEFT:
	    p.x = left;
	    p.y = top;
	    break;
	case UPPER_RIGHT:
	    p.x = right - dm.widthPixels;
	    p.y = top;
	    break;
	case LOWER_LEFT:
	    p.x = left;
	    p.y = bottom - dm.heightPixels;
	    break;
	case LOWER_RIGHT:
	    p.x = right - dm.widthPixels;
	    p.y = bottom - dm.heightPixels;
	    break;
	default:
	    if (left <= 0) {
		p.x = left;
	    } else if (right >= dm.widthPixels) {
		p.x = right - dm.widthPixels + (1 << 16);
	    } else {
		int width = right - left;
		p.x = ((left << 16) + (dm.widthPixels - width) / 2)
			/ (dm.widthPixels - width);
	    }
	    if (top <= 0) {
		p.y = top;
	    } else if (bottom >= dm.heightPixels) {
		p.y = bottom - dm.heightPixels + (1 << 16);
	    } else {
		int height = bottom - top;
		p.y = ((top << 16) + (dm.heightPixels - height) / 2)
			/ (dm.heightPixels - height);
	    }
	}
	return p;
    }
}
